package cn.benma666.sjsj.myutils;

import cn.benma666.iframe.BasicObject;
import cn.benma666.iframe.Conf;
import cn.benma666.myutils.WebUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * * 通用拦截器
 */
public class CommonHandlerInterceptor  extends BasicObject implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取真实ip
        final String realIpAddr = WebUtil.getRealIpAddr(request);
        if(!isBlank(Conf.getUtilConfig().getLogin().getIpxz())
                &&!realIpAddr.matches(Conf.getUtilConfig().getLogin().getIpxz())){
            log.info("{}ip不在限制范围内", realIpAddr);
            //ip校验
            WebUtil.sendJson(response,failed("你不在ip限制范围内", realIpAddr));
            return false;
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
